package itrans;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.sql.DataSource;
import java.sql.*;
import java.util.*;

import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
import org.apache.commons.dbcp.datasources.*;

public class DeleteStop extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("Get Stops does not support GET requests");
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException
	{
		process(request, response);
	}
	
	public void process(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException
	{
		HttpSession session = request.getSession(true);
		UserBean userBean = (UserBean) session.getAttribute(PublicConstants.USERBEAN_ATTR);
		StopBean stopBean = (StopBean) session.getAttribute(PublicConstants.STOPBEAN_ATTR);
		deleteStop(userBean, stopBean);
	}
	
	private void deleteStop(UserBean userBean, StopBean stopBean)
	{
		Connection con;    
		String searchQuery = "DELETE FROM MyStops where user_id = ? and stopid = ?";

		try {
		  DataSource dbcp = (DataSource)getServletContext().getAttribute("dbpool");
		  con = dbcp.getConnection();
		  
		  PreparedStatement query = con.prepareStatement(searchQuery);
		  query.setInt(1, userBean.getId());
		  query.setString(2, stopBean.getStopId());
		  
		  query.executeUpdate();
		  query.close();
		  con.close();
		}
		catch(SQLException ex) {
			System.out.println("SQLException: " + ex.getMessage());
		}
		catch (Exception e) {
		  e.printStackTrace();
		}
  }
}
